//
//  DMTabBarViewController.m
//  MCNKuaiYi
//
//  Created by Mr.Cannon on 2017/1/7.
//  Copyright © 2017年 Mr.Cannon. All rights reserved.
//

#import "DMTabBarViewController.h"
#import "DMNavigationController.h"
@interface DMTabBarViewController ()

@end

@implementation DMTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
#pragma mark: - 初始化界面
-(void)setupUI
{
    //创建主页
    UIViewController* homeController = [self controllerWithClassName:@"DMHomeViewController" andTitle:@"首页" andImageName:@"tab_normal_1"];
    //创建发现页面
    UIViewController* DiscoverController = [self controllerWithClassName:@"DMDiscoverViewController" andTitle:@"发现" andImageName:@"tab_normal_2"];
    //创建健康页面
    UIViewController* HealthyController = [self controllerWithClassName:@"DMHealthyViewController" andTitle:@"健康" andImageName:@"tab_normal_3"];
    self.viewControllers = @[homeController,DiscoverController,HealthyController];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark: - 封装创建Controller界面
-(UIViewController *)controllerWithClassName:(NSString *)clsName andTitle:(NSString *)title andImageName:(NSString *)imgName{
    
    Class cls = NSClassFromString(clsName);
    UIViewController* viewController = [[cls alloc]init];
    //设置文字
    viewController.title = title;
    //设置选中文字颜色
    [viewController.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName:DMRGBCOLOR} forState:UIControlStateHighlighted];
    //设置默认图片
    viewController.tabBarItem.image = [UIImage imageNamed:imgName];
    
    //设置选中图片
    UIImage *selectImg = [UIImage imageNamed:[imgName stringByAppendingString:@"_selected"]];
    
    //用原始图片来设置为homeVC的选中图片
    viewController.tabBarItem.selectedImage = selectImg;
    //调用创建item方法
    [self setTabberItem:viewController];
    DMNavigationController *navigationController = [[DMNavigationController alloc] initWithRootViewController:viewController];
    
    return navigationController;
}
#pragma mark: -创建item
-(void)setTabberItem:(UIViewController*)controller
{
    //创建button
    UIButton* leftItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 25, 25)];
    //设置图片
    [leftItem setBackgroundImage:[UIImage imageNamed:@"menusnew"] forState:UIControlStateNormal];
    controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftItem];
    //添加事件
    [leftItem addTarget:self action:@selector(leftItemAction:) forControlEvents:UIControlEventTouchUpInside];
}
//点击事件
-(void)leftItemAction:(UIButton*)sendder
{
    [self.drawer open];
}
//侧滑视图将要出现的时候关闭用户交互
-(void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}
//侧滑视图结束的时候开启用户交互
-(void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}
@end
